import { Physics, Scene, Time, Math } from "phaser";
import { type Main } from "../Main";

export class Snowman extends Physics.Arcade.Sprite {
    // 尺寸：Big 大雪人, Small 小雪人
    size: string;
    // 速度
    speed: number = 50;
    // 所处轨道序号
    trackIndex: number;
    // 行为：1 空闲, 2 走路, 3 投掷雪球
    action: number = 1;
    // 选择计时器
    chooseTimer: Time.TimerEvent;

    scene: Main = null;

    constructor(scene: Main, size: string, y: number, trackIndex: number) {
        const frame = size === "Small" ? "snowman-small-idle0" : "snowman-big-idle0";
        const x = size === "Small" ? 120 : -100;
        // 创建对象
        super(scene, x, y, "sprites", frame);
        this.scene = scene;
        scene.add.existing(this);
        scene.physics.add.existing(this);

        // 设置属性
        this.trackIndex = trackIndex;
        this.size = size;

        // 调整身体范围和原点
        if (size === "Small") {
            this.setOrigin(0.78, 1);
            this.body.setSize(90, 90);
            this.body.setOffset(20, 44);
        } else {
            this.setOrigin(0.83, 1);
            this.body.setSize(120, 120);
            this.body.setOffset(30, 50);
        }
        // 雪人空闲动画
        this.play("snowmanIdle" + size);

        // 注册事件
        this.addEvent();
    }
    // 注册事件
    addEvent() {
        // 雪人投掷动画事件
        this.on("animationcomplete-snowmanThrowStart" + this.size, this.throwStart, this);
        this.on("animationcomplete-snowmanThrowEnd" + this.size, this.throwEnd, this);
    }
    start() {
        // 没有选择计时器，说明未被用户击中，开启走路
        if (!this.chooseTimer) {
            this.walk();
        }
    }
    // 选择行为
    chooseAction() {
        this.enableBody();
        // 雪人还未出现，先走路
        if (this.x < 0) {
            this.walk();
            return;
        }
        // 随机生成与上次不同的行为
        let preAction = this.action;
        while (preAction == this.action) {
            let t = Math.Between(1, 100);
            //  1 - 40 = 投掷雪球（40%）
            //  41 - 60 = 空闲（20%）
            //  61 - 100 = 走路（40%）
            this.action = t <= 40 ? 3 : t > 60 ? 2 : 1;
        }
        switch (this.action) {
            case 1:
                this.idle();
                break;
            case 2:
                this.walk();
                break;
            case 3:
                this.throw();
                break;
        }
    }
    // 雪人行为1：空闲
    idle() {
        this.setVelocityX(0);
        this.play("snowmanIdle" + this.size, true);
        this.chooseTimer = this.scene.time.delayedCall(Math.Between(2000, 4000), this.chooseAction, [], this);
    }
    // 雪人行为2：走路
    walk() {
        this.setVelocityX(this.speed);
        this.play("snowmanWalk" + this.size, true);
        this.chooseTimer = this.scene.time.delayedCall(Math.Between(3000, 6000), this.chooseAction, [], this);
    }
    // 雪人行为3：投掷雪球
    throw() {
        this.setVelocityX(0);
        this.play("snowmanThrowStart" + this.size);
    }
    // 投掷开始
    throwStart() {
        if (!this.body.enable) {
            return;
        }
        this.play("snowmanThrowEnd" + this.size);
        // 回调回场景
        this.scene.throwSnowmanBall(this.trackIndex, this.x);
    }
    // 投掷结束
    throwEnd() {
        if (!this.body.enable) {
            return;
        }
        this.idle();
    }
    // 雪人被打
    hited() {
        if (this.chooseTimer) {
            this.chooseTimer.remove();
        }
        // 禁用
        this.disableBody();
        this.action = null;

        // 被打动画和音效
        this.play("snowmanDie" + this.size);
        this.scene.sound.play("hit-snowman");
        // 向后滑行
        this.scene.tweens.add({
            targets: this,
            x: "-=" + Math.Between(100, 200),
            ease: "sine.out",
            duration: 1000,
            onComplete: () => {
                if (this.x < -100) {
                    this.x = -100;
                }
            },
        });
        // 重新选择行为
        this.chooseTimer = this.scene.time.delayedCall(Math.Between(1500, 3000), this.chooseAction, [], this);
    }
    preUpdate(time: number, delta: number) {
        super.preUpdate(time, delta);
        // 雪人走到头，游戏结束
        if (this.x >= 950) {
            this.scene.gameOver();
        }
    }
    // 停止
    stop() {
        if (this.chooseTimer) {
            this.chooseTimer.remove();
        }
        this.disableBody(true);
        return this;
    }
}
